package engine.scene.player
{
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.data.SceneObject;
	import engine.flag.PlayerActionFlag;
	import engine.logic.walk.NormalMoveLogic;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.text.NxLabel;
	import newx.com.loader.SwfLoader;

	/**
	 *	宠物模型 
	 * @author lw
	 * 
	 */
	public class PetModel extends MoveAction
	{
		private var _modelLocator:ModelLocator;
		private var _nameLabel	: NxLabel=new NxLabel();
		
		private var _starLabel:NxLabel = new NxLabel();
		
		private var _name		: String;
		
		private var _model		: String;
		
		protected var _moveLogic	: NormalMoveLogic;
		
		private var _master	: PlayerModel;
		
		private var _xPoint	: int;
		
		private var _width		: int;
		
		private var _playerObj:SceneObject;
		
		private var _petname:String;
		private var _petlevel:int;

		public function PetModel()
		{
			_moveLogic = new NormalMoveLogic();
			_moveLogic.speed = 5;
			
			mouseChildren = mouseEnabled = false;
			_modelLocator = ModelLocator.getInstance();
		}

		public function get playerObj():SceneObject
		{
			return _playerObj;
		}

		public function set playerObj(value:SceneObject):void
		{
			_playerObj = value;
		}

		public function set model(value:String):void
		{
			if( _model == value )
			{
				return;
			}
			_model =StaticIni.getIniVar(value,"Model");
			var url:String = ResPath.ACTORS + "npc_" + _model+ ".swf";
//			ResManager.poorLoadRes(url,url, onLoadResComplete,[ _model ] );
			SwfLoader.StartLoad( ResPath.ACTORS + "npc_" + _model+ ".swf", null, onLoadResComplete, [ _model ] );
		}
		
		private function setNamePos():void
		{
			if( null != _playerMovie &&_playerMovie.hasOwnProperty("overhead"))
			{
				
				_nameLabel.y	=	_playerMovie.overhead.y - _nameLabel.height;
				_nameLabel.x	=	_playerMovie.overhead.x - _nameLabel.width;
				_starLabel.x	=	_nameLabel.x;
				_starLabel.y	=	_nameLabel.y + 15;
			}
		}
		
		public function chaseMove():void
		{
			if( null == _playerMovie )
			{
				return;
			}
			
			_moveLogic.startMove( this, new Point( x, y ),[ new Point( -_master.direction * _width * .5, 0 )] );
		}
		
		override public function set status(value:String):void
		{
			switch(value)
			{
				case PlayerActionFlag.NormalWait:
				case PlayerActionFlag.Sit:
					petWait();
					break;
				case PlayerActionFlag.Run:
					petRun();
					break
			}
		}
		//npc加载完成
		private function onLoadResComplete( movie:DisplayObject,pList:Array ):void
		{
			//没有资源
			if( null == movie )return;
			
			if( null != _playerMovie && null != _playerMovie.parent )
			{
				_playerMovie.parent.removeChild( _playerMovie );
			}
			
			_playerMovie = movie as MovieClip;
			_playerMovie.gotoAndPlay(PlayerActionFlag.Wait);
			_width = _playerMovie.width;
			addChild( _playerMovie );
			addChild(_nameLabel);
			addChild(_starLabel);
			setNamePos();
			mouseChildren=mouseEnabled=true;
			x = -_master.direction * _playerMovie.width * .5;
		}
		
		 public function set petName(str:String):void
		{
			 setNamePos();
			 _petname = str;
			 _nameLabel.htmlText = "Lv." + _petlevel + " " + _petname;
		}
		 
		 public function set petGrade(grade:int):void
		 {
			 setNamePos();
			 var str:String = "";
			 for(var i:int = 0; i < grade; i++){
				 str += "★";
			 }
			 _starLabel.text = str;
		 }
		 
		 public function set petLevel(level:int):void
		 {
			 setNamePos();
			 _petlevel = level;
			 _nameLabel.htmlText = "Lv." + _petlevel + " " + _petname;
		 }
		/**	未启用待机*/
		private function petNormalWait():void
		{
			playMc( PlayerActionFlag.NormalWait );
		}
		
		/**	启用待机*/
		private function petWait():void
		{
			playMc( PlayerActionFlag.Wait );
		}
		
		/**	高兴开始*/
		private function petDelight():void
		{
			playMc( PlayerActionFlag.Delight );
		}
		
		/**	战斗跑步*/
		private function petRun():void
		{
			playMc( PlayerActionFlag.Run );
		}
		
		private function playMc( frameName : String ):void
		{
			if( _playerMovie != null && _playerMovie.currentLabel == frameName ) return;

			try
			{
				_playerMovie.gotoAndPlay( frameName );
			}catch(e:Error){};
		}
		
		public function clear():void
		{
			_nameLabel.text = "";
			_starLabel.text = "";
			
			if( null != _playerMovie && null != _playerMovie.parent )
			{
				_playerMovie.parent.removeChild( _playerMovie );
			}
			
			_playerMovie = null;
			
			if( null != parent )
			{
				parent.removeChild( this );
			}
		}

		public function set master(value:PlayerModel):void
		{
			_master = value;
			
			direction = -_master.direction;
		}

		override public function get width():Number
		{
			return _width;
		}
	}
}